from django.utils.decorators import method_decorator

from gis.common.django_ext.decorators import validate_parameters
from gis.common.django_ext.views import BaseView
from itpay.core.agency import receipt_service, agency_service, quick_reply_service, blacklist_service
from itpay.core.agency.const import ComplaintTypeCN, COMPLAIN_REQUIRE_MSG
from itpay.core.recharge_order import order_service
from itpay.core.recharge_order.const import RechargeSourceType, OrderStatus
from itpay.core.user import user_service
from . import form

H5_PAY_URI = ''


class PayTypesView(BaseView):
    @method_decorator(validate_parameters(form.GetPayTypesSchema))
    def get(self, request, cleaned_data):
        return receipt_service.get_agency_valid_pay_types_by_user(**cleaned_data)


class MatchReceiptAddressView(BaseView):
    @method_decorator(validate_parameters(form.MatchBestReceiptByPayTypeSchema))
    def get(self, request, cleaned_data):
        by_agency = bool(cleaned_data.get('by_agency'))
        if 'by_agency' in cleaned_data:
            del cleaned_data['by_agency']
        receipt_address = receipt_service.match_receipt_address_pay_type(**cleaned_data, check_auto_show=not by_agency)
        return receipt_address


class OrderListView(BaseView):
    @method_decorator(validate_parameters(form.CreateOrderSchema))
    def post(self, request, cleaned_data):
        order = order_service.create_order(**cleaned_data, source=RechargeSourceType.IM_AUTO)
        return order


class GetPlayerInfo(BaseView):
    @method_decorator(validate_parameters(form.GetPlayerInfoSchema))
    def get(self, request, cleaned_data):
        user_info = user_service.get_user_info(cleaned_data['mch_id'], cleaned_data['user_id'])
        return {
            'id': cleaned_data['user_id'],
            'level': user_info['level'],
            'success_order_count': order_service.get_order_count(**cleaned_data, order_status=OrderStatus.SUCCESS),
            'is_blacklist': blacklist_service.check_in_blacklist(**cleaned_data)
        }


class AgencyInfoView(BaseView):
    @method_decorator(validate_parameters(form.GetAgencyInfoSchema))
    def get(self, request, cleaned_data):
        agency = agency_service.get_agency_by_id(cleaned_data['agency_id'])
        return dict(id=agency['id'], prologue=agency['prologue'])


class AgencyOffWorkView(BaseView):
    @method_decorator(validate_parameters(form.AgencyOffWorkSchema))
    def post(self, request, cleaned_data):
        agency_service.agency_go_off_work(**cleaned_data, remark='off work by im')
        return dict(id=cleaned_data['agency_id'])


class AgencyQuickReplyView(BaseView):
    @method_decorator(validate_parameters(form.AgencyQuickReplySchema))
    def get(self, request, cleaned_data):
        _, quick_reply = quick_reply_service.list_quick_reply(1, 5, cleaned_data['agency_id'])
        return quick_reply


class BlacklistView(BaseView):
    @method_decorator(validate_parameters(form.AddBlacklistSchema))
    def post(self, request, cleaned_data):
        blacklist_service.add_blacklist_user(**cleaned_data)


class BlacklistCheckView(BaseView):
    @method_decorator(validate_parameters(form.CheckBlacklistSchema))
    def get(self, request, cleaned_data):
        return dict(is_blacklist=blacklist_service.check_in_blacklist(**cleaned_data))


class ComplaintAgencyView(BaseView):
    @method_decorator(validate_parameters(form.CreateComplaintSchema))
    def post(self, request, cleaned_data):
        if cleaned_data["images"]:
            cleaned_data["images"] = cleaned_data["images"].split("|")

        cleaned_data["complaint_types"] = [cleaned_data["complaint_types"]]
        complain = agency_service.complaint_agency(**cleaned_data)
        agency = agency_service.get_agency_by_id(complain['agency_id'])

        message = COMPLAIN_REQUIRE_MSG.format(agency_name=agency["nickname"],
                                              complain_type=",".join(
                                                  [ComplaintTypeCN[t] for t in complain["complaint_types"]]),
                                              complain_content=complain['complaint_content']
                                              )
        return dict(id=complain['id'], message=message)
